﻿using UnityEngine;
using System.Collections;


public class GameScreenScript : MonoBehaviour {

	private float size_w = 75, size_h = 75;
	private float pos_x = 75, pos_y = 50;
	public static int ShapesAmount = 7;

	public Texture2D[] shapes_menu = new Texture2D[ShapesAmount];
	public Texture2D[] shapes_chosen = new Texture2D[ShapesAmount];

	public Texture2D TileSelectionBackground;

	public GameObject[] shapes = new GameObject[ShapesAmount];

	public Texture2D pause_button;

	private int shapePressed = -1;

	void OnGUI ()
	{
		GUI.DrawTexture ( new Rect(50, 50, TileSelectionBackground.width, TileSelectionBackground.height), TileSelectionBackground );

		if(GUI.Button(new Rect(Screen.width - pause_button.width - 50, 50, pause_button.width, pause_button.height), pause_button))
		{
			Application.LoadLevel ("PauseMenu");
		}

		for(int i = 0; i < ShapesAmount; ++i)
		{
			if(GUI.Button (new Rect(pos_x, pos_y + (i*72), size_w, size_h), shapes_menu[i], GUIStyle.none))
			{
				shapePressed = i;
			}
			else if(shapePressed == i)
			{
				GUI.DrawTexture ( new Rect(pos_x, pos_y + (i*72), size_w, size_h), shapes_chosen[i] );
			}
		}
	}

	// Use this for initialization
	void Start ()
	{
	
	}

	private GameObject obj;

	// Update is called once per frame
	void Update ()
	{
		Vector3 temp = Input.mousePosition; temp.z = 5;
		Vector3 worldPos = Camera.main.ScreenToWorldPoint ( temp );
		worldPos.z = 0;

		if(obj) obj.transform.position = worldPos;

		if( Input.GetMouseButtonDown(0) )
		{
			if(shapePressed != -1)
			{
				for(int i = 0; i < ShapesAmount; ++i)
				{
					if(shapePressed == i)
					{
						obj = Instantiate ( shapes[i], worldPos, Quaternion.identity) as GameObject; 
						obj.rigidbody.useGravity = false;
						obj.collider.enabled = false;
						shapePressed = -1;
						break;
					}
				}
			}

		}
		else if( Input.GetMouseButtonUp (0) )
		{
			if(obj)
			{
				obj.rigidbody.useGravity = true;
				obj.collider.enabled = true;
				obj = null;
			}
		}
	}
}
